package br.org.acessobrasil.portal.persistencia;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.transaction.annotation.Transactional;
/**
 * Todos os Dao(s) deste pacote devem extender
 * @author Fabio Issamu Oshiro
 *
 * @param <T>
 */
@Transactional
public abstract class SuperDao<T> {
	private EntityManager em;

	@PersistenceContext
	public void setEntityManager(EntityManager em) {
		this.em = em;
	}
	public EntityManager getEntityManager() {
		return em;
	}
	public void create(T obj) throws Exception{
		getEntityManager().persist(obj);
	}
	public void delete(T obj) throws Exception{
		getEntityManager().remove(obj);
	}
	public void update(T obj) throws Exception{
		getEntityManager().merge(obj);
	}
	@Transactional(readOnly=true)
	public abstract T select(Long id) throws Exception;
	
	/**
	 * Nao implementado
	 * @return null
	 */
	@Transactional(readOnly=true)
	public List<T> list(){
		return null;
	}
	public void delete(Long id) throws Exception{
		T obj = select(id);
		delete(obj);
	}
}
